package com.yilian.xlmusic.android.view;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.yilian.xlmusic.android.R;
import com.yilian.xlmusic.android.base.BaseFragment;
import com.yilian.xlmusic.android.utils.HistoryRecord;
import com.yilian.xlmusic.android.utils.music.DiffDemoCallback;
import com.yilian.xlmusic.android.utils.music.MediaUtil;
import com.yilian.xlmusic.android.utils.net.AppConstants;
import com.yilian.xlmusic.android.view.adapter.HintAdapter;
import com.yilian.xlmusic.android.view.adapter.HistoryAdapter;
import com.yilian.xlmusic.android.view.adapter.ItemRecycleViewAdapter;
import com.yilian.xlmusic.android.view.bean.HistoryPresenter;
import com.yilian.xlmusic.android.view.bean.MusicListBean;
import com.yilian.xlmusic.android.view.bean.MusicPlayerBean;
import com.yilian.xlmusic.android.view.bean.SearchHintBean;
import com.yilian.xlmusic.android.view.bean.SharedMessage;
import com.yilian.xlmusic.android.view.bean.SharedViewModel;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import rxhttp.RxHttp;

public class SearchFragment extends BaseFragment {
    private SmartRefreshLayout recycler_smart;
    private RecyclerView recycler_list;
    private SharedViewModel model;
    HistoryPresenter historyPresenter = new HistoryPresenter();
    @Override
    public int getLayoutResId() {
        return R.layout.recycleview_list;
    }

    @Override
    public void initBindInject(Object target, View view) {
        recycler_smart = view.findViewById(R.id.recycler_smart);
        recycler_list = view.findViewById(R.id.recycler_list);
    }

    @Override
    public void init(Bundle savedInstanceState) {
        model = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
        model.getSelected().observe(getViewLifecycleOwner(), value -> {
            switch (value.getType()) {
                case 1:
                    upDataUI(value.getValue());
                    break;
                case 2:
                    historyPresenter.goSearch(getActivity(),value.getValue(),model,recycler_list);
                    break;
            }
        });
        initData();
        historyPresenter.showHistory(getActivity(),recycler_list,model);
    }
    private void upDataUI(String value) {
        if (TextUtils.isEmpty(value)) {
            historyPresenter.showHistory(getActivity(),recycler_list,model);
        } else {
            showHint(value);
        }
    }

    /**
     * @param value 提示关键字
     */
    private void showHint(String value) {
        AppConstants.METHOD_NAME = "提示关键字：";
        RxHttp.get("/ylmusic-gateway/search/keywords")
                .addHeader(AppConstants.ACCESS_TOKEN_KEY, AppConstants.ACCESS_TOKEN)
                .add("keywords", value)
                .asClass(SearchHintBean.class)
                .observeOn(AndroidSchedulers.mainThread())  //主线程回调观察者
                .subscribe(s -> {
                    List<String> stringList = s.getData();
                    HintAdapter hintAdapter = new HintAdapter(R.layout.item_hint, stringList, value,model);
                    recycler_list.setAdapter(hintAdapter);
                }, throwable -> {
                    Log.e("请求失败了", "请求失败了" + throwable);
                });
    }

    private void initData() {
        recycler_list.setLayoutManager(new LinearLayoutManager(mActivity));
    }


    @Override
    public void onPause() {
        super.onPause();
        MediaUtil.getInstance().pause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MediaUtil.getInstance().stop();
    }
}
